<?php
class registration extends controller{
	public function __construct($url){
		parent::__construct($url);
	}

	public function check(){
		if(isset($_SESSION["user"]) && !empty($_SESSION["user"])) $this->redirect("start");
		if(!isset($this->url[1]) || empty($this->url[1])) $this->redirect("error/ticket");
		$this->id = $this->database->fetchRow("SELECT id FROM hash WHERE hash='".$this->url[1]."'");
		if(count($this->id) != 1) $this->redirect("error/ticket");
		$this->id = $this->id[0][0];
	}

	public function run(){
		$this->registration();
	}

	private function registration(){
		if(isset($_POST["registration"])){
			if(!empty($_POST["name"]) && !empty($_POST["email"]) && !empty($_POST["username"]) && !empty($_POST["password"]) && !empty($_POST["status"])){
				if($_POST["password"] == $_POST["password2"]){
					$sql = "INSERT INTO user (name, email, username, password, status)
							VALUES('".$_POST["name"]."', '".$_POST["email"]."', '".$_POST["username"]."', '".md5($_POST["password"])."', ".$_POST["status"].")";
					if($this->database->query($sql)){
						$this->database->query("DELETE FROM hash WHERE id=".$this->id);
						$this->redirect("login", "Sikeresen regisztrált a rendszerünkbe!");
					}
					else{
						$this->message = "Adatbázis hiba történt a regisztráció során!";
					}
				}
				else{
					$this->message = "A jelszó és a megerősítése nem egyezik meg!";
				}
			}
			else{
				$this->message = "Üresen hagyott mező!";
			}
		}
	}
}
?>